<?php
/**
 * 
 * @author luhaoz-work
 *
 */
class AuctionCache extends CFormModel{
    
    public $auction_id;
    public $start_time;
    public $end_time;
    public $winner_price_increment;
    public $winner_price;
    public $winner_time;
    public $winner_id;
    public $status;
    
    public $title;
    public $featured;
       
    public $image;
    
    protected $_location_cache;
    
    const AUCTION_CACHE_DATA = 'auction_cache_data';
    const AUCTION_CACHE_INDEX = 'auction_cache_index';
    
    public function rules(){
        return array(
            array('auction_id,start_time,end_time,winner_price_increment,winner_price,winner_time,winner_id,status,title,featured,image','safe')
        );
    }
    
    public function getCacheDataKey($auctionId){
        return self::AUCTION_CACHE_DATA.'_'.$auctionId;
    }
    
    public function getCacheIndexKey($auctionId){
        return self::AUCTION_CACHE_INDEX.'_'.$auctionId;
    }
    
    public function findByPk($auctionId){
        $this->auction_id = $auctionId;
        $cacheData = Yii::app()->cache->get($this->getCacheDataKey($this->auction_id));
        $this->attributes = $cacheData;
        return $this->attributes;
    }
    

    public function findIndexByPk($auctionId){
        $this->auction_id;
        return Yii::app()->cache->get($this->getCacheIndexKey($this->auction_id));
    }
        
    public function findAll($ids){
        $auctionIds = array();
        $findAllData = array();
        foreach ($ids as $id){
            $auctionIds[] = $this->getCacheDataKey($id);
        }       
        $auctionTempData = Yii::app()->cache->mget($auctionIds);
        if(!empty($auctionTempData)){
            foreach ($auctionTempData as $auctionData){
                $className = get_class($this);
                $auctionCache = new $className();
                $auctionCache->attributes = $auctionData;
                $findAllData[] = $auctionCache;
            }
        }
        return $findAllData;
    }
    
    public function findIndexAll($ids){
        $auctionIndexIds = array();
        $findIndexAllData = array();
        foreach ($ids as $id){
            $auctionIds[] = $this->getCacheIndexKey($id);
        }
        return Yii::app()->cache->mget($auctionIds);       
    }
    
    public function findIndexAllNC($ids){
        $buildIds = $ids;
        $findIndexs = $this->findIndexAll($ids);
        foreach ($buildIds as $index => $auctionId){
            $indexKey =  $this->getCacheIndexKey($auctionId);
            if (array_key_exists($indexKey, $findIndexs) && $findIndexs[$indexKey]){
                unset($buildIds[$index]);
            }
        }
        if (!empty($buildIds)){
            $this->buildAllByAr($buildIds);
            $findIndexs = $this->findIndexAll($ids);
        }
        return $findIndexs;
    }
    
    
    public function findAllNC($ids){
        $buildIds = $ids;
        $findData = $this->findAll($ids);
        foreach ($buildIds as $index => $auctionId){
            $auctionDataKey =  $this->getCacheDataKey($auctionId);
            if (array_key_exists($auctionDataKey, $findData) && $findData[$auctionDataKey]){
                unset($buildIds[$index]);
            }
        }
        if (!empty($buildIds)){
            $this->buildAllByAr($buildIds);
            $findData = $this->findAll($ids);
        }
        return $findData;
    }
    
    public function buildByAr($id){
        $auctionPool = new AuctionPool();

        $auctionPoolData = $auctionPool->with('auctionInfo','productResource')->findByPk($id);
        $saveData = $auctionPoolData->attributes;        
        $saveData['title'] = $auctionPoolData->auctionInfo->title;
        $this->attributes = $saveData;
        $this->image = !empty($saveData->productResource[0]->path)?$saveData->productResource[0]->path:null;
        return $this->save();
    }
    
    public function buildAllByAr($ids = array()){
        $auctionPool = new AuctionPool();    
        if (!empty($ids)){
            $auctionPoolDatas = $auctionPool->with('auctionInfo','productResource')->findAllByPk($ids);
        }else{
            $auctionPoolDatas = $auctionPool->with('auctionInfo','productResource')->findAll();
        }
        
        foreach ($auctionPoolDatas as $auctionPoolData){
            $className = get_class($this);
            $auctionCache = new $className();
            $saveData = $auctionPoolData->attributes;
            $saveData['title'] = $auctionPoolData->auctionInfo->title;
            $auctionCache->attributes = $saveData;
            $auctionCache->image = !empty($saveData->productResource[0]->path)?$saveData->productResource[0]->path:null;
            $auctionCache->save();
        }
        return array();
    }
    
    public function save(){
        if ($this->status == 2){
            $expire = time() + 86400;
        }else{
            $expire = $this->end_time - time() + 86400;
        }
        
        Yii::app()->cache->set($this->getCacheIndexKey($this->auction_id),md5(microtime().':'.$this->auction_id));
        return Yii::app()->cache->set($this->getCacheDataKey($this->auction_id),$this->attributes);
    }
}